<template>
    <div>
        <router-view></router-view>
    </div>
</template>

<script>
import config from '@/js/util/config.js'; // 加载引用不能删
import bus from '@/js/event/bus.js';
import PATH from '@/js/util/path.js';
import scene from '@/js/main/scene.js';
import user from '@/js/util/user.js';

export default {
    beforeCreate() {
        window.getRootName = function() {
            return 'DTS_CHARTS_';
        };
    },
    created() {
        user.setToken('dtscharts');
        user.setUploadToken('dtscharts_upload');
        bus.on('initExporePlayer', this.initExporePlayer);
    },
    destroyed () {
        bus.off('initExporePlayer', this.initExporePlayer);
    },
    methods: {
        initExporePlayer(data) {
            let apiOptions = {
                'onReady': async e =>{
                    // this.getViewportSize();
                    if (data && data.viewer) {
                        this.$store.commit("setMainUIVisibility", false);
                        return false;
                    }
                    // explorer集合，如果有id直接进入编辑页面，没有id也进入编辑页面 自动保存
                    // let params = await this.$store.dispatch('getChartsParams');
                    let params = {};
                    let obj = {};
                    if (data) {
                        obj = Object.assign({}, params, data);
                    } else {
                        obj = Object.assign({}, params);
                    }
                    if (obj && obj.id) {
                        scene.setID(obj.id);
                        this.$router.push({ path: PATH.HOME, query: { layout: obj.layout ? obj.layout : '1', model: obj.model ? obj.model : '2' } });
                    } else {
                        // this.$router.push(PATH.SCENE);
                        this.$router.push({ path: PATH.HOME, query: { layout: '1' } });
                    }
                },
                'onApiVersion': this.onAirCityApiVersion,
                'onEvent': this.onAirCityEvent,
                'onLog': this.onAirCityLog,
                'useColorLog': false
            };
            window.__g = new DigitalTwinAPI('127.0.0.1:4322', apiOptions);
        },
        onAirCityEvent(event) {
            if (event.eventtype == 'LeftMouseButtonClick') {
                this.$store.commit('setClickInfo', event);
            }
        },
        onAirCityLog(a) {},
        onAirCityApiVersion(a, b) {},
        async getViewportSize() {
            let res = await window.ue.internal.execute('viewportSize');
            if (JSON.parse(res)) {
                this.$store.commit('setViewportSize', JSON.parse(res));
            } else {
                this.$store.commit('setViewportSize', [1920, 1080]);
            }
        }
    }
};
</script>